package com.tulingxueyuan.mall.modules.pms.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tulingxueyuan.mall.common.api.CommonPage;
import com.tulingxueyuan.mall.common.api.CommonResult;
import com.tulingxueyuan.mall.dto.AttrWithCidDTO;
import com.tulingxueyuan.mall.modules.pms.model.PmsProductAttribute;
import com.tulingxueyuan.mall.modules.pms.model.PmsProductCategory;
import com.tulingxueyuan.mall.modules.pms.service.PmsProductAttributeService;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 商品属性参数表 前端控制器
 * </p>
 *
 * @author XuShu
 * @since 2023-05-08
 */
@RestController
@RequestMapping("/productAttribute")
public class PmsProductAttributeController {
    @Resource
    private PmsProductAttributeService pmsProductAttributeService;

    @GetMapping("/list/{cid}")
    @ApiOperation("获取商品分类的属性或参数")
    public CommonResult<CommonPage<PmsProductAttribute>> list(
            @PathVariable(value = "cid") Integer cid,
            @RequestParam(value = "type",required = false) Integer type,
            @RequestParam(value = "pageNum",required = false,defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize",required = false,defaultValue = "5") Integer pageSize){
        Page page = pmsProductAttributeService.listPage(cid, type, pageNum, pageSize);
        return CommonResult.success(CommonPage.restPage(page));
    }

    @PostMapping("/create")
    @ApiOperation("添加商品属性或者参数")
    public CommonResult add(@RequestBody PmsProductAttribute pmsProductAttribute){
        boolean save = pmsProductAttributeService.add(pmsProductAttribute);
        if (save){
            return CommonResult.success(true);
        }
        return CommonResult.failed();
    }

    @GetMapping("/{id}")
    @ApiOperation("根据id查询参数或者属性")
    public CommonResult<PmsProductAttribute> getById(@PathVariable(value = "id") Integer id){
        PmsProductAttribute pmsProductAttribute = pmsProductAttributeService.getById(id);
        return CommonResult.success(pmsProductAttribute);
    }

    @PostMapping("/update/{id}")
    @ApiOperation("根据id查询参数或者属性")
    public CommonResult update(@PathVariable(value = "id") Integer id,@RequestBody PmsProductAttribute pmsProductAttribute){
        boolean b = pmsProductAttributeService.updateById(pmsProductAttribute);
        if (b){
            return CommonResult.success(b);
        }
        return CommonResult.failed();
    }

//    如果用穿过是数据想要用集合接收 必须用@RequestParam注解标注值
    @PostMapping("/delete")
    @ApiOperation("根据id删除属性或者参数")
    public CommonResult delete(@RequestParam("ids") List<Long> ids){
        boolean b = pmsProductAttributeService.delete(ids);
        if (b){
            return CommonResult.success(b);
        }
        return CommonResult.failed();
    }

    //    如果用穿过是数据想要用集合接收 必须用@RequestParam注解标注值
    @GetMapping("/attrInfo/{cid}")
    @ApiOperation("根据id删除属性或者参数")
    public CommonResult<List<AttrWithCidDTO>> getAttrWithList(@PathVariable(value = "cid") Integer cid){
        List<AttrWithCidDTO> list = pmsProductAttributeService.getAttrWithList(cid);
        return CommonResult.success(list);
    }

}

